<?php 
function wx_jssdk($debug = false){
    
    $agent = $_SERVER['HTTP_USER_AGENT'];

    preg_match("/MicroMessenger\\/(\\w+\\.\\w+\\.\\w+)[\\s\\S]/", $agent, $agentres);

    if(empty($agentres)) {
        register_jssdk($debug);
        return 0;
    }

    $version = str_replace('.', '', $agentres[1]);

    // 老版本微信
    if($version < 672) {
        register_jssdk($debug);
        return $version;
    }

    global $_W;
    
    if (defined('HEADER')) {
        echo '';
        return;
    }
    
    $sysinfo = array(
        'uniacid'   => $_W['uniacid'],
        'acid'      => $_W['acid'],
        'siteroot'  => $_W['siteroot'],
        'siteurl'   => $_W['siteurl'],
        'attachurl' => $_W['attachurl'],
        'cookie'    => array('pre'=>$_W['config']['cookie']['pre'])
    );
    if (!empty($_W['acid'])) {
        $sysinfo['acid'] = $_W['acid'];
    }
    if (!empty($_W['openid'])) {
        $sysinfo['openid'] = $_W['openid'];
    }
    if (defined('MODULE_URL')) {
        $sysinfo['MODULE_URL'] = MODULE_URL;
    }
    $sysinfo = json_encode($sysinfo);
    $jssdkconfig = json_encode($_W['account']['jssdkconfig']);
    $debug = $debug ? 'true' : 'false';
    
    $script = <<<EOF

<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script type="text/javascript">
    window.sysinfo = window.sysinfo || $sysinfo || {};
    
    // jssdk config 对象
    jssdkconfig = $jssdkconfig || {};
    
    // 是否启用调试
    jssdkconfig.debug = $debug;
    
    jssdkconfig.jsApiList = [
        'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'hideMenuItems',
        'showMenuItems',
        'hideAllNonBaseMenuItem',
        'showAllNonBaseMenuItem',
        'translateVoice',
        'startRecord',
        'stopRecord',
        'onRecordEnd',
        'playVoice',
        'pauseVoice',
        'stopVoice',
        'uploadVoice',
        'downloadVoice',
        'chooseImage',
        'previewImage',
        'uploadImage',
        'downloadImage',
        'getNetworkType',
        'openLocation',
        'getLocation',
        'hideOptionMenu',
        'showOptionMenu',
        'closeWindow',
        'scanQRCode',
        'chooseWXPay',
        'openProductSpecificView',
        'addCard',
        'chooseCard',
        'openCard',
        'updateAppMessageShareData',
        'updateTimelineShareData'
    ];
    
    wx.config(jssdkconfig);
    
</script>
EOF;
    echo $script;
    return $version;
}

/**
 * 创建分享js
 * params['name'] 为操作标识，不可设置重复的自定义参数
 */
function js_share($title, $desc, $link, $imgUrl = '', $params = []) {
    global $_W;
    $version = $_W['jssdk_version'];
    if(empty($version)) {
        $version = 0;
    }

    $imgUrl = $imgUrl ?: "$('img')[0].src";

    $params['et'] = 'wx_share';
    $url = purl('notice', $params);

    // 老版jssdk处理
    if($version < 672) {
        $logic = "data.success=callback;wx.onMenuShareAppMessage(data);wx.onMenuShareTimeline(data);";
    } else {
        $logic = "wx.updateAppMessageShareData(data,callback);wx.updateTimelineShareData(data,callback);";
    }

    $tmp = "wx.ready(function(){var shareData={title:'{$title}',desc:'{$desc}',link:'{$link}',imgUrl:{$imgUrl}};
        function shareCallback(res){if(res.errMsg=='onMenuShareAppMessage:ok'||res.errMsg=='sendAppMessage:ok'){
            $.get('{$url}')            
        }}wxshare(shareData,shareCallback)});function wxshare(data,callback,version){{$logic}}";

    return $tmp;
}